package com.ccjk.beusoft.thirdlib.plugin2;

import java.lang.reflect.Method;

/**
 * Created by CCJK on 2017/12/22.
 */

public class AndroidGetter {
    Object mActivityThread;
    Object mInstrumentation;

    private AndroidGetter() {

    }

    private static class Instance {
        private static AndroidGetter instance = new AndroidGetter();
    }

    public static AndroidGetter get() {
        return Instance.instance;
    }

    public Object getActivityThread() {
        if (mActivityThread == null) {
            try {
                Class clazz = Class.forName("android.app.ActivityThread");
                Method currentActivityThreadMethod = clazz.getDeclaredMethod("currentActivityThread");
                currentActivityThreadMethod.setAccessible(true);
                mActivityThread = currentActivityThreadMethod.invoke(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mActivityThread;
    }

    public Object getInstrumentation() {
        if (mInstrumentation == null) {
            try {
                Class clazz = Class.forName("android.app.ActivityThread");
                Method getInstrumentationMethod = clazz.getDeclaredMethod("getInstrumentation");
                getInstrumentationMethod.setAccessible(true);
                mInstrumentation = getInstrumentationMethod.invoke(getActivityThread());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mInstrumentation;

    }

}
